{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c9a79c88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已保存到 WDRC_Table.csv\n",
      "生成数据维度：(129, 56) (129行×56列)\n",
      "前5行数据示例：\n",
      "    0dB   2dB   4dB   6dB   8dB  10dB  12dB  14dB  16dB  18dB  ...  92dB  \\\n",
      "0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  ...   0.0   \n",
      "1  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  ...   0.0   \n",
      "2  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  ...   0.0   \n",
      "3  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  ...   0.0   \n",
      "4  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  40.0  ...   0.0   \n",
      "\n",
      "   94dB  96dB  98dB  100dB  102dB  104dB  106dB  108dB  110dB  \n",
      "0   0.0   0.0   0.0    0.0    0.0    0.0    0.0    0.0    0.0  \n",
      "1   0.0   0.0   0.0    0.0    0.0    0.0    0.0    0.0    0.0  \n",
      "2   0.0   0.0   0.0    0.0    0.0    0.0    0.0    0.0    0.0  \n",
      "3   0.0   0.0   0.0    0.0    0.0    0.0    0.0    0.0    0.0  \n",
      "4   0.0   0.0   0.0    0.0    0.0    0.0    0.0    0.0    0.0  \n",
      "\n",
      "[5 rows x 56 columns]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 定义分段函数\n",
    "def generate_curve(x):\n",
    "    # if x <= 15:\n",
    "    #     return -20\n",
    "    # if 15 < x <= 30:\n",
    "    #     return -20 + (x - 15) * (50/15)  # 从-20均匀上升到40\n",
    "    # elif 30 < x <= 40:\n",
    "    #     return 30\n",
    "    # elif 40 < x <= 90:\n",
    "    #     return 30 - (x - 40) * (30/50)  # 从40均匀下降到0\n",
    "    # else:\n",
    "    #     return 0\n",
    "    if x <= 40:\n",
    "        return 40\n",
    "    elif 40 < x <= 90:\n",
    "        return 40 - (x - 40) * (40/50)  # 从40均匀下降到0\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "# 生成x轴数据 (0,2,4,...,110)\n",
    "x_values = np.arange(0, 111, 2)  # 共56个点\n",
    "\n",
    "# 计算对应的y值\n",
    "y_values = np.array([generate_curve(x) for x in x_values])\n",
    "\n",
    "# 创建129行相同数据的DataFrame\n",
    "data = np.tile(y_values, (129, 1))\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "# 添加列名 (频率值)\n",
    "df.columns = [f\"{x}dB\" for x in x_values]\n",
    "\n",
    "# 导出到CSV\n",
    "csv_path = \"WDRC_Table.csv\"\n",
    "df.to_csv(csv_path, index=False)\n",
    "\n",
    "print(f\"数据已保存到 {csv_path}\")\n",
    "print(f\"生成数据维度：{df.shape} (129行×{len(x_values)}列)\")\n",
    "print(\"前5行数据示例：\")\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "37643169",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Excel file format cannot be determined, you must specify an engine manually.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 7\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;66;03m# 1. 读取 Excel 文件\u001b[39;00m\n\u001b[1;32m      6\u001b[0m file_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWDRC_Table.csv\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 7\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_excel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheader\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# 第一行作为列名\u001b[39;00m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;66;03m# 2. 转换为 numpy 数组（去掉列名部分）\u001b[39;00m\n\u001b[1;32m     10\u001b[0m data \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39mto_numpy()\n",
      "File \u001b[0;32m~/miniconda3/envs/HA_Test_39/lib/python3.9/site-packages/pandas/io/excel/_base.py:495\u001b[0m, in \u001b[0;36mread_excel\u001b[0;34m(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)\u001b[0m\n\u001b[1;32m    493\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(io, ExcelFile):\n\u001b[1;32m    494\u001b[0m     should_close \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 495\u001b[0m     io \u001b[38;5;241m=\u001b[39m \u001b[43mExcelFile\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    496\u001b[0m \u001b[43m        \u001b[49m\u001b[43mio\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    497\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstorage_options\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstorage_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    498\u001b[0m \u001b[43m        \u001b[49m\u001b[43mengine\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    499\u001b[0m \u001b[43m        \u001b[49m\u001b[43mengine_kwargs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mengine_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    500\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    501\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m engine \u001b[38;5;129;01mand\u001b[39;00m engine \u001b[38;5;241m!=\u001b[39m io\u001b[38;5;241m.\u001b[39mengine:\n\u001b[1;32m    502\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    503\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEngine should not be specified when passing \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    504\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man ExcelFile - ExcelFile already has the engine set\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    505\u001b[0m     )\n",
      "File \u001b[0;32m~/miniconda3/envs/HA_Test_39/lib/python3.9/site-packages/pandas/io/excel/_base.py:1554\u001b[0m, in \u001b[0;36mExcelFile.__init__\u001b[0;34m(self, path_or_buffer, engine, storage_options, engine_kwargs)\u001b[0m\n\u001b[1;32m   1550\u001b[0m     ext \u001b[38;5;241m=\u001b[39m inspect_excel_format(\n\u001b[1;32m   1551\u001b[0m         content_or_path\u001b[38;5;241m=\u001b[39mpath_or_buffer, storage_options\u001b[38;5;241m=\u001b[39mstorage_options\n\u001b[1;32m   1552\u001b[0m     )\n\u001b[1;32m   1553\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m ext \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1554\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   1555\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExcel file format cannot be determined, you must specify \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1556\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124man engine manually.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   1557\u001b[0m         )\n\u001b[1;32m   1559\u001b[0m engine \u001b[38;5;241m=\u001b[39m config\u001b[38;5;241m.\u001b[39mget_option(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mio.excel.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mext\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.reader\u001b[39m\u001b[38;5;124m\"\u001b[39m, silent\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m   1560\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m engine \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "\u001b[0;31mValueError\u001b[0m: Excel file format cannot be determined, you must specify an engine manually."
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 1. 读取 Excel 文件\n",
    "file_path = \"WDRC_Table.csv\"\n",
    "df = pd.read_excel(file_path, header=0)  # 第一行作为列名\n",
    "\n",
    "# 2. 转换为 numpy 数组（去掉列名部分）\n",
    "data = df.to_numpy()\n",
    "\n",
    "# 3. 确认尺寸\n",
    "num_fft = data.shape[0]  # 应为 129\n",
    "max_freq = 8000  # Hz\n",
    "\n",
    "# 4. 生成频率坐标\n",
    "freqs = np.linspace(0, max_freq, num_fft)\n",
    "\n",
    "# 5. 获取横坐标标签（来自表头）\n",
    "x_labels = df.columns.tolist()\n",
    "\n",
    "# 6. 绘制图像\n",
    "plt.figure(figsize=(10, 6))\n",
    "extent = [0, len(x_labels) - 1, 0, max_freq]\n",
    "\n",
    "# 用imshow画二维矩阵（注意纵坐标是频率）\n",
    "plt.imshow(data, aspect='auto', origin='lower', extent=extent)\n",
    "plt.colorbar(label='Amplitude')\n",
    "\n",
    "# 设置坐标轴\n",
    "plt.xticks(ticks=np.arange(len(x_labels)), labels=x_labels, rotation=45, ha='right')\n",
    "plt.yticks(np.linspace(0, max_freq, 9))  # 0,1000,...,8000\n",
    "plt.xlabel(\"Columns from Header\")\n",
    "plt.ylabel(\"Frequency (Hz)\")\n",
    "plt.title(\"FFT Curve (output_curve.xlsx)\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "HA_Test_39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
